require 'rho/rhocontroller'
require 'helpers/browser_helper'
require 'json'
class QADetailController < Rho::RhoController
  include BrowserHelper
  
  # GET /QADetail
  def index
    question_id = @params["question_id"]
    user_id = @params["teacher_id"]
    
    response = Rho::AsyncHttp.get(
      :url => WEBSERVER_URL + "get_conversation",
      :body => "user_id=#{user_id}&question_id=#{question_id}"
    )

    @teacher_id = @params["teacher_id"]
    @data = response["body"]
    render :action => :index
  end

  def submit_qa_detail
    user_id = $user["id"]
    question_id = @params["question_id"]
    reply_to = @params["other_user_id"]
    content = @params["content"]
    param_string = {
      :qa_detail => {
        :user_id => user_id,
        :question_id => question_id,
        :content => content,
        :rely_to => reply_to
      }
    }.to_json
    Rho::AsyncHttp.post(
      :url => WEBSERVER_URL + "create_detail",
      :headers => {"content-type" => "application/json"},
      :body => param_string
    )

    redirect url_for(:action => :index, :query => {:question_id => question_id, :teacher_id => user_id})
  end



  def get_conversations
    @question_id = @params["question_id"]
    user_id = $user["id"]
      
    response = Rho::AsyncHttp.get(
          :url => WEBSERVER + url,
          :body => "user_id=#{user_id}&question_id=#{question_id}"
        )
    @conversations = response["body"]["data"]
     
    render :action => :conversation_list    
  end
  # GET /QADetail/{1}
  def show
    @qadetail = QADetail.find(@params['id'])
    if @qadetail
      render :action => :show, :back => url_for(:action => :index)
    else
      redirect :action => :index
    end
  end

  # GET /QADetail/new
  def new
    @qadetail = QADetail.new
    render :action => :new, :back => url_for(:action => :index)
  end

  # GET /QADetail/{1}/edit
  def edit
    @qadetail = QADetail.find(@params['id'])
    if @qadetail
      render :action => :edit, :back => url_for(:action => :index)
    else
      redirect :action => :index
    end
  end

  # POST /QADetail/create
  def create
    @params["qa_detail"] = {
      :user_id => $user["id"],
      :to => @params["other_user_id"],
      :question_id => @params["question_id"],
      :content => @params["content"]    
    }
    
    Rho::AsyncHttp.post(
      :url => WEBSERVER_URL + "create_qa_detail",
      :body => @params.to_json
    )
    
    redirect :action => :index
  end

  # POST /QADetail/{1}/update
  def update
    @qadetail = QADetail.find(@params['id'])
    @qadetail.update_attributes(@params['qadetail']) if @qadetail
    redirect :action => :index
  end

  # POST /QADetail/{1}/delete
  def delete
    @qadetail = QADetail.find(@params['id'])
    @qadetail.destroy if @qadetail
    redirect :action => :index  
  end
  



end
